package jgroups.demos;

import java.net.InetAddress;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.jgroups.protocols.BARRIER;
import org.jgroups.protocols.FD_ALL;
import org.jgroups.protocols.FD_SOCK;
import org.jgroups.protocols.FRAG2;
import org.jgroups.protocols.MERGE2;
import org.jgroups.protocols.MFC;
import org.jgroups.protocols.PING;
import org.jgroups.protocols.UDP;
import org.jgroups.protocols.UFC;
import org.jgroups.protocols.UNICAST3;
import org.jgroups.protocols.VERIFY_SUSPECT;
import org.jgroups.protocols.pbcast.GMS;
import org.jgroups.protocols.pbcast.NAKACK2;
import org.jgroups.protocols.pbcast.STABLE;
import org.jgroups.stack.ProtocolStack;
import org.jgroups.util.Util;

/**
 * @author Bela Ban
 */
public class ProgrammaticChat {

	public static void main(String[] args) throws Exception {
		JChannel ch = new JChannel(false);
		ProtocolStack stack = new ProtocolStack();
		ch.setProtocolStack(stack);
		stack.addProtocol(
				new UDP().setValue("bind_addr",
						InetAddress.getByName("192.168.1.5")))
				.addProtocol(new PING())
				.addProtocol(new MERGE2())
				.addProtocol(new FD_SOCK())
				.addProtocol(
						new FD_ALL().setValue("timeout", 12000).setValue(
								"interval", 3000))
				.addProtocol(new VERIFY_SUSPECT()).addProtocol(new BARRIER())
				.addProtocol(new NAKACK2()).addProtocol(new UNICAST3())
				.addProtocol(new STABLE()).addProtocol(new GMS())
				.addProtocol(new UFC()).addProtocol(new MFC())
				.addProtocol(new FRAG2());
		stack.init();

		ch.setReceiver(new ReceiverAdapter() {
			public void viewAccepted(View new_view) {
				System.out.println("view: " + new_view);
			}

			public void receive(Message msg) {
				System.out.println("<< " + msg.getObject() + " ["
						+ msg.getSrc() + "]");
			}
		});

		ch.connect("ChatCluster");

		for (;;) {
			String line = Util.readStringFromStdin(": ");
			ch.send(null, line);
		}
	}

}
